package com.world.business.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.world.business.user.domain.UserKungFu;
import com.world.business.user.domain.query.UserKungFuAddRequest;
import com.world.business.user.domain.query.UserKungFuPageRequest;
import com.world.business.user.domain.query.UserKungFuUpdateRequest;
import com.world.business.user.domain.vo.UserKungFuVO;
import com.world.business.user.enums.UserKungFuLevelEnum;
import com.world.business.user.mapper.UserKungFuMapper;
import com.world.business.user.service.IUserKungFuService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户所习功法 服务实现类
 * </p>
 *
 * @author 邓驴
 * @since 2023-09-05
 */
@Service
public class UserKungFuServiceImpl extends ServiceImpl<UserKungFuMapper, UserKungFu> implements IUserKungFuService {

    @Override
    public Long add(UserKungFuAddRequest request) {
        UserKungFu userKungFu = BeanUtil.copyProperties(request, UserKungFu.class);
        if (userKungFu.getLevel() == null) {
            userKungFu.setLevel(UserKungFuLevelEnum.RU_MEN.getLevel());
        }
        this.baseMapper.insert(userKungFu);
        return userKungFu.getId();
    }

    @Override
    public IPage<UserKungFuVO> getPage(IPage<UserKungFu> page, UserKungFuPageRequest request) {
        return this.baseMapper.getPage(page, request);
    }

    @Override
    public void update(UserKungFuUpdateRequest request) {
        LambdaUpdateWrapper<UserKungFu> updateWrapper = Wrappers.lambdaUpdate(UserKungFu.class);
        updateWrapper.eq(UserKungFu::getId, request.getId());
        updateWrapper.set(request.getUserId() != null, UserKungFu::getUserId, request.getUserId());
        updateWrapper.set(request.getKungFuId() != null, UserKungFu::getKungFuId, request.getKungFuId());
        updateWrapper.set(request.getLevel() != null, UserKungFu::getLevel, request.getLevel());
        this.baseMapper.update(null, updateWrapper);
    }

}
